﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using Google.GData.Client;

namespace Journal.Helpers
{
    /// <summary>
    /// Helper class used to invoke various MessageBox instances with localized strings.
    /// </summary>
    class MessageBoxHelper
    {
        public static void ShowAuthenticationErrorMessage()
        {
            string text = Utilities.GetLocalizedValue("Journal", "Locale", "GoogleLogin_AuthFail_Message");
            string caption = Utilities.GetLocalizedValue("Journal", "Locale", "GoogleLogin_AuthFail_Caption");
            MessageBox.Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        public static void ShowConnectionErrorMessage()
        {
            string text = Utilities.GetLocalizedValue("Journal", "Locale", "GoogleLogin_RequestFail_Message");
            string caption = Utilities.GetLocalizedValue("Journal", "Locale", "GoogleLogin_RequestFail_Caption");
            MessageBox.Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        public static void ShowCriticalErrorMessage()
        {
            string text = Utilities.GetLocalizedValue("Journal", "Locale", "CriticalError_Message");
            string caption = Utilities.GetLocalizedValue("Journal", "Locale", "CriticalError_Caption");
            MessageBox.Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public static void ShowExceptionFromWorkerError(RunWorkerCompletedEventArgs e)
        {
            Exception ex = e.Error;
            if(ex.GetType().Equals(typeof(GDataRequestException)))
            {
                GDataRequestException gdre = ex as GDataRequestException;
                if (gdre.ResponseString != null && gdre.ResponseString.Contains("Error=BadAuthentication"))
                {
                    MessageBoxHelper.ShowAuthenticationErrorMessage();
                }
                else if (gdre.ResponseString == null && ex.InnerException.Message.Contains("'www.google.com'"))
                {
                    MessageBoxHelper.ShowConnectionErrorMessage();
                }
            }
            else
            {
                MessageBoxHelper.ShowCriticalErrorMessage();
            }
        }
    }
}
